/*
 * AssemblerApplication1.asm
 *
 *  Created: 2/14/2014 8:11:04 PM
 *   Author: Yosemit Xolalpa Rosales
 */ 
 .NOLIST
 .INCLUDE "C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR Assembler\Native\2.1.39.1005\avrassembler\include\m328def.inc"
 .LIST

;initializing the stack
LDI R16, HIGH(RAMEND)
OUT SPH, R16
LDI R16, LOW(RAMEND)
OUT SPL, R16

;begin loading numbers onto the stack.
	LDI R17, 20			; num of loops (loop counter)
	LDI R16, 31			; start at 31
LoadingLoop:			; we're generating a list of 20 numbers
	PUSH R16
	INC R16				; increment
	DEC R17				; decrement counter
	BRNE LoadingLoop	; while counter ~ 0 loop! (checks SREGISTER)

;prepare registers for use in divisible loop
	LDI R17, 20			; initialize loop counter
;registers indicated in ASSIGNMENT
	LDI R20, 0			; initialize DIV3 sum
	LDI R21, 0
	LDI R23, 0			; initialize DIV5 sum
	LDI R24, 0

ParseLoop:
	POP R16
	MOV R18, R16		; place in my FIRST *sketch* register
	MOV R19, R16		; place in my SECOND *sketch* register

;"Parse through numbers and add all those divisible by 3"
;Place results in R20:R21
	divthree:
		LDI R22, 3		; load our 3
		SUB R18, R22	; subtract from first *sketch*
		BRMI divfive	; JUMP! if result NEGATIVE
		SUBI R18, 0
		BRNE divthree	; JUMP! if num not ZERO

	sumthree:
		ADC R21, R16	; add NUM to DIV3 sum
		BRCC divfive	; JUMP! if carry flag is clear
		LDI R18, 1		; use first *sketch* register
		ADD R20, R18	; add carry (if carry flag is set)

;"Parse through numbers and add all those divisible by 5"
;Place results in R23:R24
	divfive:
		LDI R22, 5		; load our 5
		SUB R19, R22	; subtract from second *sketch*
		BRMI nextnum	; JUMP! if result NEGATIVE
		SUBI R19, 0
		BRNE divfive	; JUMP! if num not ZERO

	sumfive:
		ADC R24, R16	; add NUM to DIV5 sum
		BRCC nextnum	; JUMP! if carry flag is clear
		LDI R19, 0			; use second *sketch* register
		ADD R23, R19	; add carry (if carry flag is set)

	nextnum:
		DEC R17			; decrement loop counter
		BRNE ParseLoop	; while counter ~ 0 loop! (checks SREGISTER)
/*
---- TRON IS AWESOME ----
*/